﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;

using ProjManagSolution.pmMyWebService;

namespace ProjManagSolution
{
    public partial class Register : Page
    {
        public Register()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void buttCancel_Click(object sender, RoutedEventArgs e)
        {
            PageSwitch ps = this.Parent as PageSwitch;
            ps.Navigate(new MainPage());
        }

        protected bool Input_Validate()
        {
            if (txtboxEmail.Text == "")
            {
                MessageBox.Show("Email cannot be empty!");
                return false;
            }
            // Password empty
            else if (passboxPassword.Password == "")
            {
                MessageBox.Show("Password cannot be empty!");
            }

            //password match check
            else if (passboxConfirmPassword.Password != passboxPassword.Password)
            {
                MessageBox.Show("Password not match!");
                return false;
            }

            //email empty
            

            // 
            else if (chkboxTermOfService.IsChecked != true)
            {
                MessageBox.Show("You must agree with our terms of service to become a member!");
                return false;
            }

            return true;
        }

    
           
        private void buttRegisterDone_Click(object sender, RoutedEventArgs e)
        {
            //check user exist
            pmWebServiceClient webs = new pmWebServiceClient();
            webs.GetUserDetailByEmailCompleted += new EventHandler<GetUserDetailByEmailCompletedEventArgs>(webs_GetUserDetailByEmailCompleted);
            webs.GetUserDetailByEmailAsync(txtboxEmail.Text.ToString());
        }

        void webs_GetUserDetailByEmailCompleted(object sender, GetUserDetailByEmailCompletedEventArgs e)
        {
            //if no user exist, check input and add
            if (e.Result.user_email == null)
            {
                if (Input_Validate())
                {
                    user inputUser = new user();
                    inputUser.user_email = txtboxEmail.Text;
                    inputUser.user_password = passboxPassword.Password;

                    // First Name
                    if (this.txtboxFirstName.Text == "")
                        inputUser.user_firstname = "N/A";
                    else
                        inputUser.user_firstname = txtboxFirstName.Text;

                    // Last Name
                    if (this.txtboxLastName.Text == "")
                        inputUser.user_lastname = "N/A";
                    else
                        inputUser.user_lastname = txtboxLastName.Text;

                    // Bday
                    if (this.datepickBirthday.SelectedDate.HasValue == false)
                        inputUser.user_birthday = Convert.ToDateTime("1/1/1900");
                    else
                        inputUser.user_birthday = datepickBirthday.SelectedDate;

                    // Sex
                    inputUser.user_sex = this.comboSex.SelectedIndex - 1;

                    // Address
                    if (this.txtboxAddress.Text == "")
                        inputUser.user_address = "N/A";
                    else
                        inputUser.user_address = this.txtboxAddress.Text;

                    // Phone Number
                    if (this.txtboxPhoneNumber.Text == "")
                        inputUser.user_phone = "N/A";
                    else
                        inputUser.user_phone = this.txtboxPhoneNumber.Text;

                    pmWebServiceClient ws = new pmWebServiceClient();
                    ws.InsertUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_InsertUserCompleted);
                    ws.InsertUserAsync(inputUser);
                }
            }
            //if user exists
            else
            {
                MessageBox.Show("This email address is already in database!");
            }
        }

        void ws_InsertUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("User is successfully created!");
            Session s = new Session();
            s.InsertUserEmail(txtboxEmail.Text.ToString());

            PageSwitch ps = this.Parent as PageSwitch;
            ps.Navigate(new UserControlPanel());
        }

    }
}
